<?php


include('header.php');

include('loaddb.php');

include("functions.inc.php");

// get the highest number that the graph will have to show
$result=$mysql->query("select max(sums) from (select sum(totalsize) as sums from host_stats group by scanid) as scansums");
$myrow=$result->fetch_array();
$maxsize=$myrow[0];

// get the range of times
$result=$mysql->query("select unix_timestamp(min(time)), unix_timestamp(NOW()) from scans");
$myrow=$result->fetch_array();
$mintime=$myrow[0];
$maxtime=$myrow[1];

// get the widths of the scans, all but the last
$scanwidths=array();
$result=$mysql->query("SELECT oldscan.id, UNIX_TIMESTAMP( newscan.time ) - UNIX_TIMESTAMP( oldscan.time ) 
	FROM scans AS newscan, scans AS oldscan
	WHERE newscan.id = oldscan.id +1
	ORDER BY oldscan.time");
while ($myrow=$result->fetch_array())
{
	$scanwidths[$myrow[0]]=$myrow[1]/($maxtime-$mintime)*100.0;
}
// get the width of the last scan
$result=$mysql->query("SELECT scans.id, UNIX_TIMESTAMP( NOW() ) - UNIX_TIMESTAMP( scans.time ) 
	FROM scans
	ORDER BY scans.time desc
	LIMIT 1");
$myrow=$result->fetch_array();
$scanwidths[$myrow[0]]=$myrow[1]/($maxtime-$mintime)*100.0;

// get the leftover space
$result=$mysql->query("select scanid,sum(totalsize) from host_stats group by scanid");
$scangaps=array();
while ($myrow=$result->fetch_array())
{
	$scangaps[$myrow[0]]=$maxsize-$myrow[1];
}

// spit out css
?>
<style>
#nachosexygraph {
	position: absolute;
	left: 0;
	top: 0;
	width: 78%;
	height: 100%;
	}

#legend { 
	position: absolute;
	padding-left: 1em;
	left: 80%;
	top: 0;
	}
#legend .host .color {
	position: absolute;
	width: 1em;
	height: 1em;
	}
#legend .host .hostname {
	position: relative;
	left: 1em;
	}
<?php

// get the list of hosts to put in the css code
$colorpatterns=array(
	array(1,0,0),
	array(1,0.5,0),
	array(1,1,0),
	array(0,1,0),
	array(0,1,1),
	array(0,0.5,1),
	array(0,0,1),
	array(0.5,0,1),
	array(1,0,1)
	);
$colorindex=0;
$colorstrengths=array(
	255.0,
	224.0,
	180.0,
	146.0);
$strengthindex=0;

$result=$mysql->query("select hostname from hosts order by lower(hostname)");
while($myrow=$result->fetch_array())
{
	$hostname=$myrow['hostname'];
	$colorpat=$colorpatterns[$colorindex];
	$strength=$colorstrengths[$strengthindex];
	
	$color=sprintf("%02X",$colorpat[0]*$strength).sprintf("%02X",$colorpat[1]*$strength).sprintf("%02X",$colorpat[2]*$strength);

	echo ".${hostname} { background-color: #$color }\n";


	$colorindex=($colorindex+1)%count($colorpatterns);
	if ($colorindex==0)	# just repeated
		$strengthindex=($strengthindex+1)%count($colorstrengths);
}
print "</style>\n";


// get the host data
$oldscanid=-1;
$scanposition=0;
$result=$mysql->query("select scanid,hostname,totalsize from host_stats join (hosts,scans) on (host_stats.hostid=hosts.id and host_stats.scanid=scans.id) order by scans.time,lower(hostname)");
print "<div id=\"nachosexygraph\">\n";
print "<div>\n";
while ($myrow=$result->fetch_array())
{
	$scanid=$myrow['scanid']+0;
	$width=$scanwidths[$scanid];
	$widthout=sprintf("%01.6f",$width);
	$height=$myrow['totalsize']/$maxsize*100.0;
	$heightout=sprintf("%01.6f",$height);
	$hostname=$myrow['hostname'];

	if ($oldscanid!=$scanid)
	{
		echo "</div>";
		echo "<div id=\"scan$scanid\"class=\"scan\" style=\"position:absolute; left:${scanposition}%; top:0%; height:100%; width:${widthout}%\">\n";


		$gapheight=$scangaps[$scanid]/$maxsize*100.0;
		$gapheightout=sprintf("%01.6f",$gapheight);
		echo " <div class=\"gap\" style=\"height:${gapheightout}%;\"></div>\n";

		$oldscanid=$scanid;
		$scanposition+=$width;
	}
	echo " <div class=\"bar $hostname\" style=\"height:${heightout}%;\"></div>\n";
}
echo "</div>\n";
echo "</div>\n";

echo "<div id=\"legend\" >\n";
$result=$mysql->query("select hostname from hosts order by lower(hostname)");
while($myrow=$result->fetch_array())
{
	$hostname=$myrow['hostname'];
	print "<div class=\"host\"><span class=\"color $hostname\"></span><span class=\"hostname\">$hostname</span></div>\n";
}
print "</div>\n";

include("footer.php");
hitPageCounter($mysql,"servergraph");
